//
//  LXFeedBackViewController.m
//  QuickCure1
//
//  Created by Oopsjesse on 31/10/2016.
//  Copyright © 2016 maoge. All rights reserved.
//

#import "LXFeedBackViewController.h"

@interface LXFeedBackViewController ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *labelInputCount;
@property (weak, nonatomic) IBOutlet UITextView *textViewInput;

@property(nonatomic,assign)int countDown;

@end

@implementation LXFeedBackViewController


-(void)awakeFromNib{
    [super awakeFromNib];
 
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.tableView.contentInset = UIEdgeInsetsMake(30, 0, 0, 0);

    self.textViewInput.delegate = self;
    
    _countDown = 20;
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{

  
    NSString *str = [NSString stringWithFormat:@"%@%@",textView.text,text];
    
    int num = (int)[self unicodeLengthOfString:str];
    

    if (num > 10) {
        
        return NO;
    }
    
    
    return YES;

}

-(void)textViewDidChange:(UITextView *)textView{
    

    int num = (int)[self unicodeLengthOfString:textView.text];
    
    int countDown = 10 - num;
    
    self.labelInputCount.text = [NSString stringWithFormat:@"%d",countDown];
 
    
   
    
}

-(NSUInteger) unicodeLengthOfString: (NSString *) text {
    
    NSUInteger asciiLength = 0;
    
    for (NSUInteger i = 0; i < text.length; i++) {
        
        unichar uc = [text characterAtIndex: i];
        
        asciiLength += isascii(uc) ? 1 : 2;
        
    }
    NSUInteger unicodeLength = asciiLength / 2;
    
    if(asciiLength % 2) {
        
        unicodeLength++;
        
    }
    return unicodeLength;
}



- (IBAction)clickToAlert:(UIButton *)sender {
    UIAlertController *alert;
    
    if ([self.textViewInput.text isEqualToString:@""]) {
        
        alert = [UIAlertController alertControllerWithTitle:@"Warning" message:@"提交内容为空" preferredStyle:UIAlertControllerStyleAlert];
        
        
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
        
        [alert addAction:sure];
        
        
    } else {
    
    alert = [UIAlertController alertControllerWithTitle:@"恭喜你" message:@"提交成功" preferredStyle:UIAlertControllerStyleAlert];
        
         UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
             
             [self.navigationController popViewControllerAnimated:YES];
         }];
        
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:nil];
        
        [alert addAction:sure];
        
        [alert addAction:cancel];
        
    
    }
    
    [self presentViewController:alert animated:YES completion:nil];
    
    
}




- (void)viewDidLoad {
    [super viewDidLoad];


    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source



/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here, for example:
    // Create the next view controller.
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:<#@"Nib name"#> bundle:nil];
    
    // Pass the selected object to the new view controller.
    
    // Push the view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
